package org.jsola.hr.service.impl;

import org.jsola.common.StrKit;
import org.jsola.core.Page;
import org.jsola.exception.ParamException;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IEmpSocBackRecordAccountDAO;
import org.jsola.hr.dto.EmpSocBackRecordAccountAddDTO;
import org.jsola.hr.dto.EmpSocBackRecordAccountUpdateDTO;
import org.jsola.hr.entity.EmpSocBackRecordAccountDO;
import org.jsola.hr.query.EmpSocBackRecordAccountQuery;
import org.jsola.hr.service.IEmpSocBackRecordAccountService;
import org.jsola.hr.vo.EmpSocBackRecordAccountListVO;
import org.jsola.hr.vo.EmpSocBackRecordAccountVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 社保回款账户
 *
 * @author zhr
 */
@Service("hrEmpSocBackRecordAccountServiceImpl")
public class EmpSocBackRecordAccountServiceImpl implements IEmpSocBackRecordAccountService {

    @Autowired
    private IEmpSocBackRecordAccountDAO empSocBackRecordAccountDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public EmpSocBackRecordAccountVO save(EmpSocBackRecordAccountAddDTO empSocBackRecordAccountAddDTO, TokenUser tokenUser) {
        // 转DO
        EmpSocBackRecordAccountDO empSocBackRecordAccountDO = empSocBackRecordAccountAddDTO.to(EmpSocBackRecordAccountDO.class);
        // 保存
        empSocBackRecordAccountDO = save(empSocBackRecordAccountDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return empSocBackRecordAccountDO.to(EmpSocBackRecordAccountVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(EmpSocBackRecordAccountUpdateDTO empSocBackRecordAccountUpdateDTO, TokenUser tokenUser) {
        //转DO
        EmpSocBackRecordAccountDO empSocBackRecordAccountDO = empSocBackRecordAccountUpdateDTO.to(EmpSocBackRecordAccountDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(empSocBackRecordAccountDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... empSocBackRecordAccountIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) empSocBackRecordAccountIds);
    }


    @Override
    public EmpSocBackRecordAccountVO selectById(String empSocBackRecordAccountId, String siteId) {
        EmpSocBackRecordAccountDO empSocBackRecordAccountDO = selectDOById(empSocBackRecordAccountId, siteId);
        if (empSocBackRecordAccountDO == null) {
            return null;
        }
        return empSocBackRecordAccountDO.to(EmpSocBackRecordAccountVO.class);
    }

    @Override
    public List<EmpSocBackRecordAccountListVO> select(EmpSocBackRecordAccountQuery empSocBackRecordAccountQuery, String siteId) {
        List<EmpSocBackRecordAccountDO> empSocBackRecordAccountDOList = selectDO(empSocBackRecordAccountQuery, siteId);
        if(CollectionUtils.isEmpty(empSocBackRecordAccountDOList)) {
            return empSocBackRecordAccountDOList == null ? null : new ArrayList<>();
        }
        return empSocBackRecordAccountDOList.stream()
                .map(empSocBackRecordAccountDO -> empSocBackRecordAccountDO.to(EmpSocBackRecordAccountListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(EmpSocBackRecordAccountQuery empSocBackRecordAccountQuery, String siteId) {
        Example example = buildExample(empSocBackRecordAccountQuery,siteId);
        return empSocBackRecordAccountDAO.selectCountByExample(example);
    }

    @Override
    public Page<EmpSocBackRecordAccountListVO> selectPage(EmpSocBackRecordAccountQuery empSocBackRecordAccountQuery, String siteId) {
        Example example = buildExample(empSocBackRecordAccountQuery,siteId);
        Page<EmpSocBackRecordAccountDO> page = empSocBackRecordAccountDAO.selectPageByExample(example,
                empSocBackRecordAccountQuery.getPageNo(),
                empSocBackRecordAccountQuery.getPageSize());

        return page.to(EmpSocBackRecordAccountListVO.class);
    }

    @Override
    public EmpSocBackRecordAccountDO selectDOById(String empSocBackRecordAccountId, String siteId) {
        return listById(empSocBackRecordAccountId, siteId);
    }

    @Override
    public List<EmpSocBackRecordAccountDO> selectDO(EmpSocBackRecordAccountQuery empSocBackRecordAccountQuery, String siteId) {
        Example example = buildExample(empSocBackRecordAccountQuery,siteId);
        return empSocBackRecordAccountDAO.selectByExample(example);
    }

    @Override
    public EmpSocBackRecordAccountDO selectDOByEmpId(String empId, String siteId) {
        ParamException.isFalse(StrKit.isEmpty(empId), "员工id不能为空");
        Example example = new Example(EmpSocBackRecordAccountDO.class);
        example.and()
                .andEqualTo("empId", empId)
                .andEqualTo("siteId", siteId)
                .andEqualTo("valid", true);
        List<EmpSocBackRecordAccountDO> empSocBackRecordAccountDOList = empSocBackRecordAccountDAO
                .selectByExample(example);
        if (CollectionUtils.isEmpty(empSocBackRecordAccountDOList)) {
            return null;
        }
        return empSocBackRecordAccountDOList.get(0);
    }


    /**
     * 根据查询参数，构建example

     * @param empSocBackRecordAccountQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(EmpSocBackRecordAccountQuery empSocBackRecordAccountQuery, String siteId) {
        Example example = new Example(EmpSocBackRecordAccountDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (empSocBackRecordAccountQuery.getEmpId() != null) {
            example.and().andEqualTo("empId", empSocBackRecordAccountQuery.getEmpId());
        }
        // 排序
        ExampleKit.setExampleOrder(example,empSocBackRecordAccountQuery.getOrders());
        return example;
    }
}




